package com.ducanh.doan.ui.screen;

import android.app.Dialog;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import com.ducanh.doan.R;
import com.ducanh.doan.SharedPreferencesHelper;
import com.ducanh.doan.database.DBAdapter;
import com.ducanh.doan.utils.DialogUtils;
import com.ducanh.doan.utils.LanguageUtils;

public class SetupFragment extends BaseFragment {
	
	private Button btnDone, btnLanguage;
	private ArrayAdapter<String> currencyAdapter, languageAdapter;
	private String[] currencyArray, currencyCode, currencySign;
	private String[] languageArray, languageValues;
	private TypedArray languageFlags;
	private Spinner spinnerCurrency;
	private Dialog languageDialog;
	private ListView languageListView;
	private EditText editUserName;
	
	public static SetupFragment newInstance() {
		SetupFragment f = new SetupFragment();
		return f;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(getLayoutId(), container, false);
		initWidgets(view);
		addEvents();
		return view;
	}
	
	private void initWidgets(View view) {
		spinnerCurrency = (Spinner) view.findViewById(R.id.spinnerCurrency);
		spinnerCurrency.setAdapter(currencyAdapter);
		editUserName = (EditText) view.findViewById(R.id.editUserName);
		btnLanguage = (Button) view.findViewById(R.id.btnLanguage);
		btnLanguage.setText(languageArray[0]);
		btnLanguage.setCompoundDrawablesWithIntrinsicBounds(languageFlags.getDrawable(0), null, null, null);
		btnDone = (Button) view.findViewById(R.id.btnDone);			
		// dialog layout
		
		LinearLayout dialogLayout = (LinearLayout) getActivity().getLayoutInflater().inflate(R.layout.dialog_language_layout, null);
		languageListView = (ListView) dialogLayout.findViewById(R.id.lvLanguage);
		languageListView.setAdapter(languageAdapter);
		languageDialog = DialogUtils.showDialogWithOK(getActivity(), dialogLayout, getString(R.string.select_language), getString(R.string.done));
	}
	
	private void addEvents() {
		btnLanguage.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				languageDialog.show();
			}
		});
		languageListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				btnLanguage.setText(languageArray[position]);
				btnLanguage.setCompoundDrawablesWithIntrinsicBounds(languageFlags.getDrawable(position), null, null, null);
				if (languageDialog != null)
					languageDialog.dismiss();
				LanguageUtils.changeLanguage(getActivity(), languageValues[position]);
				reload();			// load lại fragment sau khi đã thay đổi ngôn ngữ
				loadResource();
				SharedPreferencesHelper pref = new SharedPreferencesHelper(getActivity().getApplicationContext());
				pref.setLocale(languageValues[position]);
			}
		});
		btnDone.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				new SharedPreferencesHelper(getActivity().getApplicationContext()).setIsFirstLaunch(false);
				
				DBAdapter db = new DBAdapter(getActivity().getApplicationContext());
				boolean ok = db.addUser(editUserName.getText()+"");
				if (ok) {
					Toast.makeText(getActivity(), getString(R.string.success), Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(getActivity(), getString(R.string.failed), Toast.LENGTH_SHORT).show();
				}
				MainFragment f = MainFragment.newInstance(null);
				startFragment(f);
			}
		});
		
	}

	@Override
	public int getLayoutId() {
		return R.layout.fragment_setup;
	}

	@Override
	public int getContainerId() {
		return R.id.main_container;
	}

	@Override
	public void loadResource() {
		currencyArray = getResources().getStringArray(R.array.currencyArray);
		currencyCode = getResources().getStringArray(R.array.currencyCode);
		currencySign = getResources().getStringArray(R.array.currencySign);
		languageArray = getResources().getStringArray(R.array.languageArray);
		languageValues = getResources().getStringArray(R.array.languageValues);
		languageFlags = getResources().obtainTypedArray(R.array.languageFlags);
		currencyAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, currencyArray);
		currencyAdapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
		languageAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_single_choice, languageArray);
	}

}
